<template>
  <div class="register-container">
    <h2>用户注册</h2>
    <form @submit.prevent="handleRegister">
      <div class="form-group">
        <label>用户名:</label>
        <input type="text" v-model="form.username" required />
      </div>
      <div class="form-group">
        <label>密码:</label>
        <input type="password" v-model="form.password" required />
      </div>
      <div class="form-group">
        <label>确认密码:</label>
        <input type="password" v-model="form.confirmPassword" required />
      </div>
      <button type="submit">注册</button>
      <p class="login-link">
        已有账号？<router-link to="/login">立即登录</router-link>
      </p>
    </form>
  </div>
</template>

<script setup>
import { ref } from "vue";
import { useRouter } from "vue-router";
import { mockRegister } from "../api/mockAuth";

const router = useRouter();
const form = ref({
  username: "",
  password: "",
  confirmPassword: "",
});

const handleRegister = async () => {
  if (form.value.password !== form.value.confirmPassword) {
    alert("两次输入的密码不一致");
    return;
  }

  const success = await mockRegister(form.value);
  if (success) {
    alert("注册成功，请登录");
    router.push("/login");
  }
};
</script>

<style scoped>
.register-container {
  max-width: 400px;
  margin: 2rem auto;
  padding: 2rem;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.form-group {
  margin-bottom: 1.5rem;
}

input {
  width: 100%;
  padding: 0.5rem;
  margin-top: 0.5rem;
}

button {
  width: 100%;
  padding: 0.75rem;
  background-color: #42b983;
  color: white;
  border: none;
  cursor: pointer;
}

.login-link {
  margin-top: 1rem;
  text-align: center;
}
</style>
